package com.lapelife.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lapelife.backend.common.ResponseResult;
import com.lapelife.backend.model.request.AuthenticateRequest;
import com.lapelife.backend.model.request.MenuRequest;
import com.lapelife.backend.model.request.RoleRequest;
import com.lapelife.backend.model.view.MenuView;
import com.lapelife.backend.model.view.RoleView;
import com.lapelife.backend.service.AuthenticateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin")
@Api(tags = "权限控制管理")
public class AuthenticateController {
    private AuthenticateService authenticateService;

    @Autowired
    public void setAuthenticateService(AuthenticateService authenticateService) {
        this.authenticateService = authenticateService;
    }

    @ApiOperation("为用户添加角色")
    @PostMapping("/role/to")
    @PreAuthorize("hasAuthority('admin:role:to')")
    public ResponseResult<String> addRoleToUser(@RequestBody AuthenticateRequest authenticateRequest) {
        return authenticateService.addRoleToUser(authenticateRequest);
    }

    @ApiOperation("添加角色")
    @PostMapping("/role/add")
    @PreAuthorize("hasAuthority('admin:role:add')")
    public ResponseResult<String> addRole(@RequestBody RoleRequest roleRequest) {
        return authenticateService.addRole(roleRequest);
    }

    @ApiOperation("修改角色信息")
    @PostMapping("/role/update")
    @PreAuthorize("hasAuthority('admin:role:update')")
    public ResponseResult<String> updateRole(@RequestBody RoleRequest roleRequest) {
        return authenticateService.updateRole(roleRequest);
    }

    @ApiOperation("查询角色信息")
    @PostMapping("/role/query")
    @PreAuthorize("hasAuthority('admin:role:query')")
    public ResponseResult<Page<RoleView>> queryRole(@RequestBody RoleRequest roleRequest) {
        return authenticateService.getRole(roleRequest);
    }

    @ApiOperation("查询权限信息")
    @PostMapping("/menu/query")
    @PreAuthorize("hasAuthority('admin:menu:query')")
    public ResponseResult<Page<MenuView>> queryRole(@RequestBody MenuRequest menuRequest) {
        return authenticateService.getMenu(menuRequest);
    }

    @ApiOperation("删除角色信息")
    @DeleteMapping("/role/delete/{id}")
    @PreAuthorize("hasAuthority('admin:role:delete')")
    public ResponseResult<String> deleteRole(@PathVariable Long id) {
        return authenticateService.deleteRoleById(id);
    }

    @ApiOperation("删除用户所拥有角色")
    @PostMapping("/role/for/delete")
    @PreAuthorize("hasAuthority('admin:role:for:delete')")
    public ResponseResult<String> deleteRoleFor(@RequestBody AuthenticateRequest authenticateRequest) {
        return authenticateService.deleteRoleFor(authenticateRequest);
    }

    @ApiOperation("为角色添加权限")
    @PostMapping("/menu/to")
    @PreAuthorize("hasAuthority('admin:menu:to')")
    public ResponseResult<String> addMenuTo(@RequestBody AuthenticateRequest authenticateRequest) {
        return authenticateService.addMenuToRole(authenticateRequest);
    }

    @ApiOperation("删除用户所拥有角色")
    @DeleteMapping("/menu/for/delete")
    @PreAuthorize("hasAuthority('admin:menu:for:delete')")
    public ResponseResult<String> deleteMenuFor(@RequestBody AuthenticateRequest authenticateRequest) {
        return authenticateService.deleteMenuFor(authenticateRequest);
    }
}
